package com.caregiver.watch.business.user.service.impl;

import com.caregiver.watch.business.user.dao.UserReminderDao;
import com.caregiver.watch.business.user.model.dto.AddUserReminderParam;
import com.caregiver.watch.business.user.model.dto.UpdateEnabledStatusParam;
import com.caregiver.watch.business.user.model.dto.UpdateUserReminderParam;
import com.caregiver.watch.business.user.model.po.UserReminder;
import com.caregiver.watch.business.user.model.vo.UserReminderVo;
import com.caregiver.watch.business.user.service.UserReminderService;
import com.caregiver.watch.business.user.utils.UserInfoUtils;
import com.caregiver.watch.common.constant.NumberConstants;
import com.caregiver.watch.common.exception.business.BusinessException;
import com.caregiver.watch.common.utils.collection.ListUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

import static com.caregiver.watch.common.constant.NumberConstants.ONE;
import static com.caregiver.watch.common.constant.NumberConstants.ZERO;

/**
 * @Description: 用户通知
 * @Author 疆戟
 * @Date 2025/8/23 15:38
 * @Version 1.0
 */
@Service
@Slf4j
public class UserReminderServiceImpl implements UserReminderService {

    @Autowired
    private UserReminderDao userReminderDao;


    @Override
    public List<UserReminderVo> queryUserReminderList() {
        Long userId = UserInfoUtils.getUserId();
        List<UserReminder> userReminders = userReminderDao.selectByUserId(userId);
        return ListUtils.map(userReminders, UserReminderVo::build);
    }

    @Override
    public Boolean addUserReminder(AddUserReminderParam param) {
        if (StringUtils.isBlank(param.getReminderName())) {
            throw new BusinessException("提醒名称不能为空");
        }
        if (StringUtils.isBlank(param.getContent())) {
            throw new BusinessException("提醒内容不能为空");
        }
        if (Objects.isNull(param.getStartTime())|| Objects.isNull(param.getEndTime())) {
            throw new BusinessException("提醒时间范围不能为空");
        }
        if (Objects.isNull(param.getIntervalValue())) {
            throw new BusinessException("提醒间隔不能为空");
        }
        if (Objects.isNull(param.getIntervalUnit())) {
            throw new BusinessException("提醒间隔单位不能为空");
        }
        if (Objects.isNull(param.getRepeatDays())) {
            throw new BusinessException("提醒重复天不能为空");
        }
        UserReminder userReminder = param.toPo();
        return userReminderDao.addUserReminder(userReminder) > ZERO;
    }

    @Override
    public Boolean deleteUserReminder(Long id) {
        if (Objects.isNull(id)) {
            throw new BusinessException("提醒ID不能为空");
        }
        Long userId = UserInfoUtils.getUserId();
        return userReminderDao.deleteUserReminder(id, userId) > ZERO;
    }

    @Override
    public Boolean updateUserReminder(UpdateUserReminderParam param) {
        if (Objects.isNull(param.getId())) {
            throw new BusinessException("提醒ID不能为空");
        }
        UserReminder userReminder = param.toPo();
        return userReminderDao.updateUserReminder(userReminder) > ZERO;
    }

    @Override
    public UserReminderVo queryUserReminder(Long id) {
        if (Objects.isNull(id)) {
            throw new BusinessException("提醒ID不能为空");
        }
        Long userId = UserInfoUtils.getUserId();
        UserReminder userReminder = userReminderDao.queryUserReminderById(id, userId);
        return UserReminderVo.build(userReminder);
    }

    @Override
    public Boolean updateEnabledStatus(UpdateEnabledStatusParam param) {
        if (Objects.isNull(param.getId())) {
            throw new BusinessException("提醒ID不能为空");
        }
        Long userId = UserInfoUtils.getUserId();
        Integer isEnabled = BooleanUtils.isTrue(param.getIsEnabled()) ? ONE : ZERO;
        return userReminderDao.updateEnabledStatus(param.getId(), userId, isEnabled) > ZERO;
    }
}
